package com.example.yangxu0703.thundermapclient.adapter;


import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.yangxu0703.thundermapclient.R;
import com.example.yangxu0703.thundermapclient.fragment.PictureFragment;
import com.example.yangxu0703.thundermapclient.util.Util;
import com.facebook.drawee.view.SimpleDraweeView;
import com.truizlop.sectionedrecyclerview.SectionedRecyclerViewAdapter;

import java.util.ArrayList;


public class PictureAdapter extends SectionedRecyclerViewAdapter<PictureAdapter.MyHeadView, PictureAdapter.MyViewHolder, PictureAdapter.MyFootView> {

    Context context;
    public ArrayList<ArrayList<String>> image_date;
    public boolean [][] check_state;
    OnItemClickListener onItemClickListener;
    public boolean Mode;
    public void setMode(boolean mode) {
        Mode = mode;
    }
    public float desity;



    public PictureAdapter(ArrayList<ArrayList<String>> image_date,boolean [][] check_state ,Context context) {
        this.context = context;
        this.image_date = image_date;
        Log.i("Adapter", image_date + "con");
        this.check_state=check_state;
        desity = context.getResources().getDisplayMetrics().density;
    }

    public void  setImageDate(ArrayList<ArrayList<String>> image_date,boolean [][] check_state){
        this.image_date = image_date;
        Log.i("PagerAdapter",check_state.length+"");
        this.check_state=check_state;
        this.notifyDataSetChanged();
    }

    @Override
    protected MyFootView onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    protected MyHeadView onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.headitem, parent, false);
        return new MyHeadView(view);
    }

    @Override
    protected MyViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout_pic, parent, false);
        return new MyViewHolder(view);
    }


    @Override
    public void onBindItemViewHolder(final MyViewHolder holder, final int section, final int position) {

        final MyViewHolder viewHolder = (MyViewHolder) holder;

        Util.loadSimpleDraweeView(Uri.parse("file://" + image_date.get(section).get(position)), viewHolder.draweeView, (int) (125 * desity), (int) (125 * desity));

        if (!Mode) {
            viewHolder.targetView.setVisibility(View.GONE);
        } else {
            viewHolder.targetView.setVisibility(View.VISIBLE);
            if (check_state[section][position]) {
                viewHolder.targetView.setBackground(context.getResources().getDrawable(R.drawable.choosed_icon));
            } else {
                viewHolder.targetView.setBackground(context.getResources().getDrawable(R.drawable.cycle));
            }
        }

        viewHolder.draweeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               onItemClickListener.onItemClick(section,position);
            }
        });

        /*viewHolder.targetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //viewHolder.targetView.setChecked(isChecked);
                if (Mode) {
                    if (check_state.get(section).get(position)) {
                        check_state.get(section).remove(position);
                        check_state.get(section).add(position,false);
                        deleteList.remove(image_date.get(section).get(position));
                    } else {
                        check_state.get(section).
                        check_state[section][position] = 1;
                        deleteList.add(image_date.get(section).get(position));
                    }
                }
            }
        });
*/
    }

    public void setOnItemClickListener(OnItemClickListener itemClick) {
        this.onItemClickListener = itemClick;
    }


    public interface OnItemClickListener {
        void onItemClick(int section, int position);
    }

    @Override
    protected void onBindSectionFooterViewHolder(MyFootView holder, int section) {

    }

    @Override
    protected void onBindSectionHeaderViewHolder(MyHeadView holder, int section) {
        MyHeadView viewHolder = (MyHeadView) holder;
        Log.i("Adapter-section", section + "Adapter");
        String[] tem = image_date.get(section).get(0).split("_");
        viewHolder.dateTV.setText(tem[1]);
    }

    @Override
    protected boolean hasFooterInSection(int section) {
        return false;
    }

    @Override
    public int getSectionCount() {
        //return photoDates.size();
        //System.out.println(MainActivity.photoDates.size()+"");
        Log.i("Adapter", PictureFragment.all_path.size() + "");
        return PictureFragment.all_path.size();
    }

    @Override
    public int getItemCount() {
        // return photos.size()+photoDates.size();
        return caculateSize(PictureFragment.all_path) + PictureFragment.all_path.size();
    }

    @Override
    protected int getItemCountForSection(int section) {
        return getCountInsection(section);
    }


    public int getCountInsection(int section) {
        Log.i("Adapter", "getCountInsection" + PictureFragment.all_path.get(section).size());
        return PictureFragment.all_path.get(section).size();
    }

    public int caculateSize(ArrayList<ArrayList<String>> path) {
        int tem = 0;
        for (ArrayList<String> list : path) {
            tem = tem + list.size();
        }
        Log.i("Adapter", tem + "Count_Size");
        return tem;
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView draweeView;
        TextView targetView;

        public MyViewHolder(View itemView) {
            super(itemView);
            draweeView = itemView.findViewById(R.id.res_pic);
            targetView = itemView.findViewById(R.id.check_pic);
        }
    }

    class MyHeadView extends RecyclerView.ViewHolder {
        TextView dateTV;

        public MyHeadView(View itemView) {
            super(itemView);
            dateTV = (TextView) itemView.findViewById(R.id.headTV);
        }
    }

    class MyFootView extends RecyclerView.ViewHolder {
        public MyFootView(View itemView) {
            super(itemView);
        }
    }

}
